/**
 * The store used for register module store
 */
Ext.define('Ext.ux.data.SimpleTreeStore', {
    extend: 'Ext.data.TreeStore',

	data: new Ext.util.MixedCollection(false, Ext.data.Store.recordIdFn),
    
    listeners: {
        load: function(me, node, records, successful, eOpts){

            me.scanFromRequestData(records, me.data , 0);
			// --- define mix collceion
			
        }
    },
    
	getAllListRecords:function() {
		return this.data
	},
	
	getTotalCount:function() {
		return this.data.length
	},

	getAt:function(index) {
		return this.data.getAt(index);
	},

    getById: function(id) {
        return (this.snapshot || this.data).findBy(function(record) {
            return record.getId() === id;
        });
    },
	
	indexOf:function(record ) {
		return this.data.indexOf(record);		
	},

    findExact: function(property, value, start) {
        return this.data.findIndexBy(function(rec) {
            return rec.get(property) === value;
        },
        this, start);
    },

    
    findBy: function(fn, scope, start) {
        return this.data.findIndexBy(fn, scope, start);
    },
		
	/**
	 * privide method
	 * @param {Object} records
	 * @param {Object} loadRecs
	 */
    scanFromRequestData: function(records, loadRecs , index){
        var me = this;
        
        for (var i = 0; i < records.length; i++) {
            var rec = records[i];
            if (!rec.get('leaf')) {
                // --- load more record ---
                
                var subRecs = rec.get('children');
				var recArray = [];
                var instance = null;

				if (subRecs) {
					
					for (var j = 0; j < subRecs.length; j++) {
	                    instance = Ext.create('orgstruct.model.DepartmentTree', {
	                        id: subRecs[j].id,
	                        text: subRecs[j].text,
	                        code: subRecs[j].code,
							leaf:subRecs[j].leaf,
							leader:subRecs[j].leader,
							disabled:subRecs[j].disabled,
							menuLevel:subRecs[j].menuLevel,
							children:subRecs[j].children
	                    });
						recArray.push(instance);
	                    
	                }
                	me.scanFromRequestData(recArray, loadRecs , index+i);					
				}
                

            }
            loadRecs.insert(index+i , rec);
        }
    },
	
    /**
     * Returns a range of Records between specified indices.
     * @param {Number} [startIndex=0] The starting index
     * @param {Number} [endIndex] The ending index. Defaults to the last Record in the Store.
     * @return {Ext.data.Model[]} An array of Records
     */
    getRange: function(start, end) {
        return this.data.getRange(start, end);
    }
    
});
